class AccountDropdownManager {
    constructor(app) {
        this.app = app;
        this.syncManager = null;
    }

    setupAccountDropdown() {
        const accountTrigger = document.getElementById('accountTrigger');
        const accountDropdown = document.querySelector('.account-dropdown');
        const accountMenu = document.getElementById('accountMenu');

        if (!accountTrigger || !accountDropdown) return;

        // 点击账号触发器
        accountTrigger.addEventListener('click', (e) => {
            e.stopPropagation();
            this.toggleAccountMenu();
        });

        // 点击菜单项
        document.querySelectorAll('.account-menu-item').forEach(item => {
            item.addEventListener('click', (e) => {
                const action = e.currentTarget.dataset.action;
                this.handleAccountAction(action);
                this.hideAccountMenu();
            });
        });

        // 点击外部关闭菜单
        document.addEventListener('click', (e) => {
            if (!accountDropdown.contains(e.target)) {
                this.hideAccountMenu();
            }
        });

        // ESC键关闭菜单
        document.addEventListener('keydown', (e) => {
            if (e.key === 'Escape') {
                this.hideAccountMenu();
            }
        });

        document.addEventListener('click', (e) => {
            if (e.target.closest('[data-action="logout"]')) {
                // 直接调用AuthManager的logout方法，它会处理确认逻辑
                this.app.authManager.logout();
            }
        });
    }

    toggleAccountMenu() {
        const accountDropdown = document.querySelector('.account-dropdown');
        if (accountDropdown.classList.contains('active')) {
            this.hideAccountMenu();
        } else {
            this.showAccountMenu();
        }
    }

    showAccountMenu() {
        const accountDropdown = document.querySelector('.account-dropdown');
        accountDropdown.classList.add('active');
    }

    hideAccountMenu() {
        const accountDropdown = document.querySelector('.account-dropdown');
        accountDropdown.classList.remove('active');
    }

    handleAccountAction(action) {
        switch (action) {
            case 'sync':
                console.log('查看同步状态');
                this.showSyncStatus();
                break;
            case 'sync-now':
                console.log('立即同步');
                this.performSyncNow();
                break;
            case 'upgrade':
                console.log('查看高级版信息');
                break;
            case 'buy-premium':
                console.log('购买高级版');
                break;
            case 'settings':
                console.log('打开账号设置');
                this.app.modalManager.showSettingsModal();
                this.app.modalManager.switchSettingsTab('general');
                break;
            case 'logout':
                this.app.authManager.logout();
                break;
            default:
                console.log('未知操作:', action);
        }
    }

    // 执行立即同步
    async performSyncNow() {
        if (!this.syncManager) {
            console.error('同步管理器未初始化');
            return;
        }

        const result = await this.syncManager.syncNow();
        
        if (result.success) {
            window.notification?.success('同步成功');
        } else {
            window.notification?.error(`同步失败：${result.message}`);
        }
    }

    // 显示同步状态
    showSyncStatus() {
        if (!this.syncManager) {
            console.error('同步管理器未初始化');
            return;
        }

        const status = this.syncManager.getSyncStatus();
        let message = '';
        
        if (!status.isLoggedIn) {
            message = '未登录，无法同步';
        } else if (!status.isOnline) {
            message = '网络离线，无法同步';
        } else if (status.isSyncing) {
            message = '正在同步中...';
        } else {
            const timeStr = this.syncManager.formatSyncTime(status.lastSyncTime);
            message = `最后同步：${timeStr}\n自动同步：${status.autoSyncEnabled ? '已开启' : '已关闭'}\n同步间隔：${status.autoSyncIntervalMinutes}分钟`;
        }
        
        window.notification?.info(message);
    }

    // 设置同步管理器
    setSyncManager(syncManager) {
        this.syncManager = syncManager;
    }
}

module.exports = AccountDropdownManager;